Setup logentries yum repo:
  file.managed:
    - name: /etc/yum.repos.d/logentries.repo
    - user: root
    - group: root
    - mode: 644
    - source: salt://logentries/files/logentries.repo

Install logentries:
  pkg.installed:
    - name: logentries

{% set le_host_header = salt['pillar.get']('jenkins:slave_env:API_URL') %}

Init logentires host key:
  cmd.run:
    - name: >
        le init
        --host-key="{{ salt['pillar.get']('logentries:host_key') }}"
        --account-key="{{ salt['pillar.get']('logentries:account_key') }}"

    - unless: test -e /etc/le/config


Register logentries host header:
  cmd.run:
    - name: le register --name="{{ le_host_header }}" --hostname="{{ le_host_header }}"
    - unless: le ls hosts/{{ le_host_header }} | grep -qv "Error"

Follow PHP Audit Trail if not registered:
  cmd.run:
    - name: >
        le follow
        "/home/lighttpd/api-storage/logs/audit-trail-20*.txt"
        --name="PHP Audit Trail"

    - unless: 'le ls hosts/{{ le_host_header }}/"PHP Audit Trail" | grep -qv "Error: Resource not found."'

Follow PHP Error if not registered:
  cmd.run:
    - name: >
        le follow
        "/home/lighttpd/api-storage/logs/error-20*.txt"
        --name="PHP Error"

    - unless: 'le ls hosts/{{ le_host_header }}/"PHP Error" | grep -qv "Error: Resource not found."'

Install logentries agent:
  pkg.installed:
    - name: logentries-daemon


Start logentries service:
  service.running:
    - name: logentries
    - enable: True
